const app = getApp();
import DateUtil from '../../utils/common/dateUtil.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      dateString: "",
      dayString:"",
      nowDate: DateUtil.getNowFormatDateSlash(),
      onwork: [],
      onrest: [],
      del: app.imgUrl+'workcal/workcal-del.png',
      nodata: app.imgUrl+'workcal/workcal-nodata.png',
      planlist:{},
      onwork: app.globalWorkDate.onwork,
      onrest: app.globalWorkDate.onrest,
    //   onwork: ['2023/06/06', '2023/06/09', '2023/06/20', '2023/06/12', '2023/07/01'],
    //   onrest: ['2023/06/07', '2023/06/14', '2023/06/22', '2023/06/16', '2023/07/03'],
    },
    dateChange(e) {
        let that = this;
        that.setData({
            dateString: e.detail.dateString,
            dayString: e.detail.dateString.substring(8,10)
        });
        
        that.getPlansByDate(e.detail.dateString);
    },
    getPlansByDate:function(fwdate){
        let that = this;
        if(app.globalPlanList.length >0 ){
            let _planlist =  app.globalPlanList.filter((item) => {
                return [item.adc252].find( (si) => {
                    return si.indexOf(fwdate) != -1;           
                });        
            });      
            that.setData({
                planlist:_planlist
            })
        }
    },
    getPlansByMonth:function(month){
        let that = this;
        let _onwork =  app.globalWorkDate.onwork.filter((item) => {
            return [item].find( (si) => {
                return si.indexOf(month) != -1;           
            });        
        }); 
        let _onrest =  app.globalWorkDate.onrest.filter((item) => {
            return [item].find( (si) => {
                return si.indexOf(month) != -1;           
            });        
        }); 
        that.setData({
            onwork:_onwork,
            onrest:_onrest
        })
    },
    delPlanById:function(e){
        let that = this;
        wx.showModal({
            title: '提示',
            content: '确定要删除该计划吗？',
            success: function (sm) {
            if (sm.confirm) {
                app.globalPlanList.splice(app.globalPlanList.findIndex(item => item.id=== e.currentTarget.dataset.id), 1);
                if(e.currentTarget.dataset.adc251 == '1'){//工作
                    app.globalWorkDate.onwork.splice(app.globalWorkDate.onwork.findIndex(item => item=== e.currentTarget.dataset.adc252), 1)
                }else if(e.currentTarget.dataset.adc251 == '2'){//休息
                    app.globalWorkDate.onrest.splice(app.globalWorkDate.onrest.findIndex(item => item=== e.currentTarget.dataset.adc252), 1)
                }
                that.setData({
                    planlist:app.globalPlanList,
                    onwork:app.globalWorkDate.onwork,
                    onrest:app.globalWorkDate.onrest
                })
                console.log('============='+JSON.stringify(that.data.planlist))
            } else if (sm.cancel) {
            }
            }
        })
    },
    monthChange(e) {
        let that = this;
        that.getPlansByMonth(e.detail.dateString.substr(0,8));
    },
    toAddPlan:function(){
        wx.showToast({
            title: '跳转添加页面',
            icon: 'loading',
            duration: 2000
        })
        wx.navigateTo({
            url: '/pages/workplan/workplan',
          })
    },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },
    
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let that = this;
        console.log('=============='+JSON.stringify(app.globalPlanList))
        that.getPlansByMonth(that.data.dateString.substr(0,8));
        that.getPlansByDate(that.data.dateString);
    },
  
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
  
    },
  
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
  
    },
  
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
  
    },
  
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
  
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
  
    }
  })